﻿/**********************************************
 * 功能名称：产品作业路径定义
编写人：  杨莹
建立时间： 杨 2009年3月17日
说明：
定义当前作业中心及中间体，目标作业中心及中间体。
 * 当最后一步的时候，目标作业中心为空。
 **************************************************************/


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using System.Text;
using System.Collections.Generic;

namespace Hisun.View.ProductInProcess
{
    public partial class PIPDiliverMap_Edit : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string alter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                HF_ProductID.Value = Request.QueryString["productid"].ToString();

                var myProductInfo = (from a in db.TABC_JS_Product
                                     where a.产品账户ID == HF_ProductID.Value
                                     select new
                                     {
                                         a.产品账户ID,
                                         a.产品账户名称
                                     }).Single();
                TextBox_ProductID.Text = HF_ProductID.Value;
                TextBox_ProductName.Text = myProductInfo.产品账户名称;

                myDataBind(HF_ProductID.Value);


            }
        }

        private void myDataBind(string ProductID) 
        {


            var myPIPInfo = from m in db.VABC_JS_ProductInProcessDelivery_Map
                            join c in db.TABC_JS_ProductInProcess
                             on m.中间体ID equals c.中间体ID
                            where c.产品ID == ProductID  orderby m.ID
                            select m ;
 


            GridView_Query.DataSource = myPIPInfo;
            GridView_Query.DataBind();   
        }
        //点击
        protected void Button_Select_Command(object sender, CommandEventArgs e)
        {

            Panel_Edit.Visible = true;

            Button CurrentButton = (Button)sender;

            DataControlFieldCell Currenfieldcell = (DataControlFieldCell)CurrentButton.Parent;
            GridViewRow gvr = (GridViewRow)Currenfieldcell.Parent;

            FillDropdownList_Start(HF_ProductID.Value);
            FillDropdownList(HF_ProductID.Value);
            DropDownList_CenterAndPIP.SelectedValue = gvr.Cells[0].Text + "&" + gvr.Cells[2].Text;
            DropDownList_GoalCenterAndPIP.SelectedValue = gvr.Cells[4].Text + "&" + gvr.Cells[6].Text;

            DropDownList_CenterAndPIP.Enabled = false;



        }

        protected void Button_Add_Click(object sender, EventArgs e)
        {
            Panel_Edit.Visible = true;


            FillDropdownList_Start(HF_ProductID.Value);
            FillDropdownList(HF_ProductID.Value);

            DropDownList_CenterAndPIP.SelectedValue = "";
            DropDownList_GoalCenterAndPIP.SelectedValue = "";

            DropDownList_CenterAndPIP.Enabled = true;
        }



        //填充目标作业中心下拉列表
        private void FillDropdownList(string ProdcutID)
        {
            DropDownList_GoalCenterAndPIP.Items.Clear();
            DropDownList_GoalCenterAndPIP.Items.Add(new ListItem("未指定", ""));

            var myCenters = (from a in db.TABC_JS_ProductInProcess
                             join b in db.TABC_JS_CostCenter on a.作业中心ID equals b.作业中心ID
                             where a.产品ID == ProdcutID
                             orderby b.作业中心名称
                             select new
                             {
                                 a.作业中心ID,
                                 b.作业中心名称,
                                 a.中间体ID,
                                 a.中间体名称


                             }).Distinct();

            foreach (var myCenter in myCenters)
            {
                DropDownList_GoalCenterAndPIP.Items.Add(new ListItem("(" + myCenter.作业中心ID.ToString() + ")" + myCenter.作业中心名称 + " | " + "(" + myCenter.中间体ID + ")" + myCenter.中间体名称, myCenter.作业中心ID.ToString() + "&" + myCenter.中间体ID));


            }



        }

        //填充当前作业中心下拉列表
        private void FillDropdownList_Start(string ProdcutID)
        {
            DropDownList_CenterAndPIP.Items.Clear();
            DropDownList_CenterAndPIP.Items.Add(new ListItem("未指定", ""));

            var myCenters = (from a in db.TABC_JS_ProductInProcess
                             join b in db.TABC_JS_CostCenter on a.作业中心ID equals b.作业中心ID
                             where a.产品ID == ProdcutID
                             orderby b.作业中心名称
                             select new
                             {
                                 a.作业中心ID,
                                 b.作业中心名称,
                                 a.中间体ID,
                                 a.中间体名称


                             }).Distinct();

            foreach (var myCenter in myCenters)
            {
                DropDownList_CenterAndPIP.Items.Add(new ListItem("(" + myCenter.作业中心ID.ToString() + ")" + myCenter.作业中心名称 + " | " + "(" + myCenter.中间体ID + ")" + myCenter.中间体名称, myCenter.作业中心ID.ToString() + "&" + myCenter.中间体ID));


            }



        }

        protected void Button_Save_Command(object sender, CommandEventArgs e)
        {
            if (DropDownList_CenterAndPIP.SelectedValue == "")
            {
              
                alter = "<script>alert('当前作业中心不能为空。');</script>";
                return;
            }
            string[] myCenterAndPIP = DropDownList_CenterAndPIP.SelectedValue.ToString().Split('&');



            //if (DropDownList_GoalCenterAndPIP.SelectedValue == "")
            //{
            //    Response.Write("<script>alert('目标作业中心不能为空。');</script>");
            //    return;
            //}
            string[] myGoalCenterAndPIP = new string[] {"",""};
            if (DropDownList_GoalCenterAndPIP.SelectedValue.ToString() == "")
            {

            }
            else 
            {
                myGoalCenterAndPIP = DropDownList_GoalCenterAndPIP.SelectedValue.ToString().Split('&');


            }


            var myQueryRows = from a in db.TABC_JS_ProductInProcessDelivery_Map
                              where a.作业中心ID.ToString() == myCenterAndPIP[0]
                              && a.中间体ID.ToString() == myCenterAndPIP[1]
                              && a.目标作业中心ID.ToString() == myGoalCenterAndPIP[0]
                              && a.目标中间体ID.ToString() == myGoalCenterAndPIP[1]
                              select a;


            if (myQueryRows.Count() == 1)
            {
                TABC_JS_ProductInProcessDelivery_Map myEditRow = myQueryRows.Single();
                myEditRow.作业中心ID = Convert.ToInt32(myCenterAndPIP[0]);
                myEditRow.中间体ID = myCenterAndPIP[1];
                if (myGoalCenterAndPIP[0] == "")
                {
                    int? x;
                    x = null;
                    myEditRow.目标作业中心ID = Convert.ToInt32(x);
                }
                else 
                {
                    myEditRow.目标作业中心ID = Convert.ToInt32(myGoalCenterAndPIP[0]);
                }
                
                
                if (myGoalCenterAndPIP[1] == "")
                {
              
                    myEditRow.目标中间体ID = null;
                }
                else
                {
                    myEditRow.目标中间体ID = myGoalCenterAndPIP[1];
                }
                myEditRow.操作员ID = Session[ServerVars.sysSessionUserName].ToString();
                myEditRow.时间 = DateTime.Now;

                try
                {
                    db.SubmitChanges();
                    //Response.Write("<script>alert('保存数据成功')</script>");
                    alter = "<script>alert('保存数据成功');</script>";
                    myDataBind(HF_ProductID.Value);

                    DropDownList_CenterAndPIP.Enabled = true;
                    Panel_Edit.Visible = false;

                }
                catch
                {

                    //Response.Write("<script>alert('保存数据失败')</script>");
                    alter = "<script>alert('保存数据失败');</script>";
                }

            }
            else if (myQueryRows.Count() == 0)
            {


                TABC_JS_ProductInProcessDelivery_Map myNewRow = new TABC_JS_ProductInProcessDelivery_Map();
                myNewRow.作业中心ID = Convert.ToInt32(myCenterAndPIP[0]);
                myNewRow.中间体ID = myCenterAndPIP[1];
                if (myGoalCenterAndPIP[0] == "")
                {
                    int? y ;
                    y = null;
                    myNewRow.目标作业中心ID = Convert.ToInt32(y);
                }
                else
                {
                    myNewRow.目标作业中心ID = Convert.ToInt32(myGoalCenterAndPIP[0]);
                }


                if (myGoalCenterAndPIP[1] == "")
                {
                    myNewRow.目标中间体ID = null;
                }
                else
                {
                    myNewRow.目标中间体ID = myGoalCenterAndPIP[1];
                }
                myNewRow.操作员ID = Session[ServerVars.sysSessionUserName].ToString();
                myNewRow.时间 = DateTime.Now;
                db.TABC_JS_ProductInProcessDelivery_Map.InsertOnSubmit(myNewRow);
                try
                {
                    db.SubmitChanges();
                    alter = "<script>alert('保存数据成功');</script>";
                    myDataBind(HF_ProductID.Value);

                    DropDownList_CenterAndPIP.Enabled = true;
                    Panel_Edit.Visible = false;

                }
                catch
                {
                    alter = "<script>alert('保存数据失败');</script>";

                }

            }

        }

        protected void Button_Cancel_Command(object sender, CommandEventArgs e)
        {

            Panel_Edit.Visible = false;
        }



        protected void Button_Del_Command(object sender, CommandEventArgs e)
        {
            long myDelId = Convert.ToInt64(e.CommandArgument.ToString());
           
            var myDelQuery = from a in db.TABC_JS_ProductInProcessDelivery_Map  where a.ID == myDelId
                              select a;
            var q2 = myDelQuery.Single();

            db.TABC_JS_ProductInProcessDelivery_Map.DeleteOnSubmit(q2);
            try
            {

                db.SubmitChanges();
              
                alter = "<script>alert('删除成功');</script>";
                myDataBind(HF_ProductID.Value);

            }
            catch
            {
                alter = "<script>alert('删除失败');</script>";

            }
        }

        protected void Button_GoBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("PIPDiliverMapSetup.aspx");
        }




    }
}
